package keni.html.android.framework.UI;

import java.util.Iterator;

import keni.html.android.framework.util.Constants;

import org.json.*;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

public class ObjectMapper {

	private String formId;
	private Context ctx;
	private Uri baseUri;

	public ObjectMapper(Context ctx, String formId) {
		this.formId = formId;
		this.ctx = ctx;
		this.baseUri = Uri.parse(Constants.ObjectProviderMetaData.CONTENT_URI);
	}

	public Uri[] addObject(String json) throws JSONException {
		JSONObject jsonObject = new JSONObject(json);
		JSONArray data = jsonObject.getJSONArray(Constants.JsonObjectKeys.DATA);
		String tableName = jsonObject.getString(Constants.JsonObjectKeys.DATASOURCENAME);
		ContentResolver contentResolver = this.ctx.getContentResolver();
		Uri[] uris = new Uri[data.length()];
		for (int i = 0; i < data.length(); i++) {
			JSONObject record = data.getJSONObject(i);
			ContentValues contentValues = new ContentValues();
			Iterator<String> keys = record.keys();
			while (keys.hasNext()) {
				String fieldName = (String) keys.next();
				contentValues.put(fieldName, jsonObject.getString(fieldName));
			}
			uris[i] = contentResolver.insert(Uri.withAppendedPath(baseUri, tableName), contentValues);
		}

		return uris;

	}

	public int updateObject(String json) throws JSONException {
		JSONObject jsonObject = new JSONObject(json);
		ContentResolver contentResolver = this.ctx.getContentResolver();
		String tableName = jsonObject.getString(Constants.JsonObjectKeys.DATASOURCENAME);
		JSONArray data = jsonObject.getJSONArray(Constants.JsonObjectKeys.DATA);
		int count = 0;
		for (int i = 0; i < data.length(); i++) {
			JSONObject record = data.getJSONObject(i);
			ContentValues contentValues = new ContentValues();
			Iterator<String> keys = record.keys();
			while (keys.hasNext()) {
				String fieldName = (String) keys.next();
				contentValues.put(fieldName, jsonObject.getString(fieldName));
			}

			if (jsonObject.has(Constants.JsonObjectKeys.WHERE)&& !jsonObject.isNull(Constants.JsonObjectKeys.WHERE)) {
				String where = jsonObject.getString(Constants.JsonObjectKeys.WHERE);
				JSONArray jsonselectionArgs = jsonObject.getJSONArray(Constants.JsonObjectKeys.SELECTIONARGS);
				String[] selectionArgs = new String[jsonselectionArgs.length()];
				for (int n = 0; n < jsonselectionArgs.length(); n++) {
					selectionArgs[n] = jsonselectionArgs.getString(n);
				}
				count += contentResolver.update(Uri.withAppendedPath(baseUri, tableName),contentValues, where, selectionArgs);
			} 
//			else {
//				String id = jsonObject.getString(Constants.FormMetadataAttributesName.ID);
//				count += contentResolver.update(Uri.withAppendedPath(Uri.withAppendedPath(baseUri, tableName), id),contentValues, null, null);
//			}

		}

		return count;

	}

	public int removeObject(String json) throws JSONException {
		JSONObject jsonObject = new JSONObject(json);
		ContentResolver contentResolver = this.ctx.getContentResolver();
		int count = 0;
		String tableName = jsonObject.getString(Constants.JsonObjectKeys.DATASOURCENAME);
		if (jsonObject.has(Constants.JsonObjectKeys.WHERE)	&& !jsonObject.isNull(Constants.JsonObjectKeys.WHERE)) {
			String where = jsonObject.getString(Constants.JsonObjectKeys.WHERE);
			JSONArray jsonselectionArgs = jsonObject.getJSONArray(Constants.JsonObjectKeys.SELECTIONARGS);
			String[] selectionArgs = new String[jsonselectionArgs.length()];
			for (int i = 0; i < jsonselectionArgs.length(); i++) {
				selectionArgs[i] = jsonselectionArgs.getString(i);
			}

			count = contentResolver.delete(Uri.withAppendedPath(baseUri, tableName), where,	selectionArgs);
		} 
//		else {
//			String id = jsonObject
//					.getString(Constants.FormMetadataAttributesName.ID);
//			count = contentResolver.delete(
//					Uri.withAppendedPath(
//							Uri.withAppendedPath(baseUri, tableName), id),
//					null, null);
//		}
		return count;
	}

	public JSONObject retrieveObject(String json) throws JSONException {
		JSONObject jsonObject = new JSONObject(json);
		ContentResolver contentResolver = this.ctx.getContentResolver();
		String tableName = jsonObject
				.getString(Constants.JsonObjectKeys.DATASOURCENAME);
		String where = null;
		String sortOrder = null;
		String[] selectionArgs = null;
		String[] projection = null;
		Uri uri = null;
		if (jsonObject.has(Constants.JsonObjectKeys.WHERE)
				&& !jsonObject.isNull(Constants.JsonObjectKeys.WHERE)) {
			where = jsonObject.getString(Constants.JsonObjectKeys.WHERE);
			JSONArray jsonselectionArgs = jsonObject
					.getJSONArray(Constants.JsonObjectKeys.SELECTIONARGS);
			selectionArgs = new String[jsonselectionArgs.length()];
			for (int i = 0; i < jsonselectionArgs.length(); i++) {
				selectionArgs[i] = jsonselectionArgs.getString(i);
			}
			uri = Uri.withAppendedPath(baseUri, tableName);
		} else {
			String id = jsonObject
					.getString(Constants.MetadataAttributesName.ID);
			uri = Uri.withAppendedPath(
					Uri.withAppendedPath(baseUri, tableName), id);
		}
		if (jsonObject.has(Constants.JsonObjectKeys.SORTORDER)
				&& !jsonObject.isNull(Constants.JsonObjectKeys.SORTORDER)) {
			sortOrder = jsonObject
					.getString(Constants.JsonObjectKeys.SORTORDER);
		}
		JSONArray fields = jsonObject
				.getJSONArray(Constants.JsonObjectKeys.FIELDS);
		projection = new String[fields.length()];
		for (int i = 0; i < fields.length(); i++) {
			projection[i] = fields.getString(i);
		}

		JSONObject outputJsonObject = new JSONObject();
		outputJsonObject.put(Constants.JsonObjectKeys.DATASOURCENAME, tableName);
		outputJsonObject.put(Constants.JsonObjectKeys.FIELDS, fields);
		Cursor cursor = null;
		try {
			
			cursor = contentResolver.query(uri, projection, where,
					selectionArgs, sortOrder);
			JSONArray dataArray = new JSONArray();
			int recordIndex = 0;
			for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
					.moveToNext()) {
				JSONObject record = new JSONObject();
				for (int i = 0; i < projection.length; i++) {
					record.put(projection[i], cursor.getString(cursor
							.getColumnIndex(projection[i])));
				}

				dataArray.put(recordIndex, record);
				recordIndex++;
			}
		} finally {
			cursor.close();
		}
		return outputJsonObject;

	}

}
